import Vue from 'vue'
// 存储localStorage时设置前缀
const storePrefix = 'RUE_';

// 存储localStorage
export const setStore = (name, content) => {
	if (!name) return;
	if (typeof content !== 'string') {
		content = JSON.stringify(content);
	}
	window.localStorage.setItem(storePrefix + name, content);
}

// 获取localStorage
export const getStore = name => {
	if (!name) return;
	return JSON.parse(window.localStorage.getItem(storePrefix + name));
}

// 删除localStorage
export const removeStore = name => {
	if (!name) return;
	window.localStorage.removeItem(storePrefix + name);
}
// 获取某个较深的对象数组里某个特定值所在的对象
export const getObjByprop=(arr,key,value)=>{
	var obj='';
	 x(arr, key, value)
	return obj
	function x(arr, key, value) {
    for (var i = 0; i < arr.length; i++) {
        if (arr[i].hasOwnProperty(key) && arr[i][key] == value) {
            return obj=arr[i]
        } else {
            for (var k in arr[i]) {
                if (Array.isArray(arr[i][k])) {
                    x(arr[i][k], key, value)
                }
            }
        }
    }
	}
}
//获取日期20171221格式当前和前几天
export const getSeriDate=(num)=>{
		var nowDate=new Date()
		var nowTimes=nowDate.getTime();
	if(num){
		var times = nowTimes-(num*24*60*60*1000)
		var oldDate=new Date(times)
		var oldMonth=oldDate.getMonth()+1
		var oldDay=oldDate.getDate()
		return ''+oldDate.getFullYear()+(oldMonth>9?oldMonth:('0'+oldMonth))+(oldDay>9?oldDay:('0'+oldDay))
	}else{
		var newMonth=nowDate.getMonth()+1
		var newDay=nowDate.getDate()
		return ''+nowDate.getFullYear()+(newMonth>9?newMonth:('0'+newMonth))+(newDay>9?newDay:('0'+newDay))
	}
}
//获取日期20171221格式
export const seriDate=(_time)=>{
		var s=new Date(_time);
		var y=s.getFullYear();
		var m=s.getMonth()+1;
		var d=s.getDate();
		return ''+y+(m>=10?m:'0'+m)+(d>=10?d:'0'+d)
	}
//匹配出科目名称或id
export const subjectMatch=(ele)=>{
	var arr=[
					{name:'数学',courseid:'13'},
					{name:'语文',courseid:'12'},
					{name:'物理',courseid:'15'},
					{name:'化学',courseid:'16'},
					{name:'生物',courseid:'17'},
					{name:'政治',courseid:'11'},
					{name:'历史',courseid:'18'},
					{name:'地理',courseid:'19'},
					{name:'英语',courseid:'14'}
				]
	
	if(!parseInt(ele)){
		if(arr.find(function(x){return x.name==ele})){		
		return arr.find(function(x){return x.name==ele}).courseid
	}else{
		return 'not find'
	}
	}else{
		if(arr.find(function(x){return x.courseid==ele})){		
		return arr.find(function(x){return x.courseid==ele}).name
	}else{
		return 'not find'
	}
	}	
}
export const bus = new Vue({})
